package org.ezplatform.office.meeting.common.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ezplatform.office.meeting.info.entity.QSUser;
import org.ezplatform.util.context.SpringContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.ezplatform.office.meeting.info.service.QSUserService;
import org.ezplatform.office.meeting.setting.base.service.BaseSetService;

import com.quanshi.openapi.QsAPI;

public class QSApiThread extends Thread {
	
	private final static Logger logger = LoggerFactory.getLogger(QSApiThread.class);
	
	private String userName;
	
	private String password;
	
	private String hostUserId;
	
	private String siteUrl;
	
	private String appId;
	
	private String p12Path;
	
	private String trustStorePath;
	
	private String emailstr;
	
	private List allUserList;

	public QSApiThread(String userName, String password, 
			           String hostUserId, String siteUrl,
			           String appId, String p12Path, 
			           String trustStorePath,String emailstr,
			           List ulist) {
		super();
		this.userName = userName;
		this.password = password;
		this.hostUserId = hostUserId;
		this.siteUrl = siteUrl;
		this.appId = appId;
		this.p12Path = p12Path;
		this.trustStorePath = trustStorePath;
		this.emailstr = emailstr;
		this.allUserList = ulist;
	}

	@Override
	public void run() {

		logger.error("------------QSApiThread start------------");
		try {
			QSUserService userservice = SpringContextHolder.getBean("QSUserService");
			//BaseSetService service = SpringContextHolder.getBean("baseSetService");
			// 初始化OpenAPI
			// 请确保证书路径正确
			QsAPI.init(appId, siteUrl, p12Path, trustStorePath);
			// 账号登陆
			//userservice.login(userName, password, false);
			
			String company="zoomlion";

			//Map<String, Object> qsUserMap = createAccount(userName, password, hostUserId,company,"JSH3");
			//logger.debug("qs_userid:"+qsUserMap.get("userId"));
			//logger.debug("qs_passwd:"+qsUserMap.get("passwd"));
			
			List qsUserList = new ArrayList();
			//List list = service.getAllUserAcc();
			if(allUserList!=null) {
				Map map = null;
				Map qsmap = null;
				QSUser user = null;
				
				String login_name = "";
				String user_name = "";
				String userId = "";
				String emailacc = "";
				
				logger.error("--allUserList.size():"+allUserList.size());
				
				for(int i=0;i<allUserList.size();i++) {
					
					map = (Map) allUserList.get(i);
					login_name = map.get("login_name")+"";
					user_name = map.get("user_name")+"";
					userId = map.get("id")+"";
					emailacc = login_name+"@"+emailstr;
					logger.error("login_name:"+login_name+"|"+i);
					//创建账号
					qsmap = userservice.createAccount(userName, password, hostUserId, company, login_name, user_name, userId, emailacc);
					qsUserList = new ArrayList();
					if(qsmap!=null) {
					   user = new QSUser();
					   user.setQsuserId(qsmap.get("userId")+"");
					   user.setQspasswd(qsmap.get("passwd")+"");
					   user.setUserAccount(map.get("login_name")+"");
					   user.setUserId(userId);
					   user.setQsuserName(user_name);
					   user.setQsemail(emailacc);
					   qsUserList.add(user);
					}
					if(qsUserList!=null&&qsUserList.size()>0) {
						userservice.saveQSUserList(qsUserList);
					}
					
				}
				
			}
			
			logger.error("--qs sync success--");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("--qs sync err--");
		}

	}
	
	
	
}
